package searchEngine;

import java.util.ArrayList;
import java.util.List;

import searcher.lucene.LuceneSearcher;


import database.mediator.MediatorInterface;
import distributedRE.model.DataBase;
import distributedRE.model.Document;
import distributedRE.model.Query;

public class LuceneSearchEngine implements SearchEngine {

	private LuceneSearcher ls;
	private DataBase db;

	public LuceneSearchEngine(DataBase db, String stopWordsFile) {
		
		ls = new LuceneSearcher(db.getIndexPath(), db.getSampleResultSize(),db.getMaxRetrievable(),stopWordsFile);
		
		this.db = db;
		
		db.setSearchEngine(this);
	}
	
	public Float getScore(Query query, Document document) {

		return Float.valueOf(0);
		
//		return ls.getScore(query.getId(), document.getPath());
	}

	public List<Document> query(Query query, MediatorInterface mI) {
		
		ls.doSearch(query.getId(), query.getMustWords(),query.getMustNotWords());

		//TODO Maybe this depends on the executor
		
		List<String> docs = ls.retrieveDocuments(query.getId());
			
		ArrayList<Document> documents = new ArrayList<Document>();
		
		for (String doc : docs) {
			
			documents.add(db.getDocument(doc,mI));
			
		}
		
		return documents;
		
	}
	
	public List<Document> queryAllAllowed(Query query, MediatorInterface mI) {
		
		ls.doSearch(query.getId(), query.getMustWords(),query.getMustNotWords());

		//TODO Maybe this depends on the executor
		
		List<String> docs = ls.retrieveMaxAllowedDocuments(query.getId());
			
		ArrayList<Document> documents = new ArrayList<Document>();
		
		for (String doc : docs) {
			
			documents.add(db.getDocument(doc,mI));
			
		}
		
		return documents;
		
	}

	public String toString(){
		return "Lucene Search Engine for: " + db.getId();
	}
}
